//
package org.whyr.android.player;

import org.whyr.android.player.PlayerService.State;
import org.whyr.android.player.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.graphics.PorterDuff; 

public class MainActivity extends Activity {

 private ImageButton playPause, stop;
 private TextView text; 
 private PlayerService.State playerState = State.stopped; 
 
 private void playerService(String s){
		Intent intent = new Intent(this, PlayerService.class);
		intent.setAction("org.whyr.android.player." + s); 
		startService(intent);
}
 private BroadcastReceiver mStatusReceiver = new BroadcastReceiver(){
	 @Override
	 public void onReceive(Context c, Intent i){
		 text.setText(i.getStringExtra("playerStatus"));
		 playerState = (PlayerService.State) i.getSerializableExtra("playerState");
		 if (playerState == State.paused || playerState == State.pausedInCall) onPaused();
		 else if (playerState == State.playing) onPlaying(); 
		 else if (playerState == State.preparing) onPreparing();
		 else if (playerState == State.stopped || playerState == State.stoppingWhilePreparing) onStopped(); 
	
		 
	 }
 }; 

 	private void onStopped(){
 		onPaused(); 
 		stop.setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY); 
 	}
	private void onPreparing() { 
		 playPause.setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY); 
		 //stop.setColorFilter(Color.LTGRAY, PorterDuff.Mode.MULTIPLY); 
		 stop.clearColorFilter(); 
	}
	private void onPlaying() {
		 playPause.clearColorFilter();
		 stop.clearColorFilter(); 
		 playPause.setImageResource(R.drawable.button_pause); 
		 playPause.setOnClickListener(new View.OnClickListener() {
			   public void onClick(View view) {
				playerService("PAUSE"); 
			   }
			  });
	}
	private void onPaused() {
		playPause.clearColorFilter();
		stop.clearColorFilter();
		playPause.setImageResource(R.drawable.button_play);
		playPause.setOnClickListener(new View.OnClickListener() {
			   public void onClick(View view) {
				playerService("PLAY"); 			
			   }
			  });
	}
  @Override
  public void onCreate(Bundle b) {
	  super.onCreate(b);
	  setContentView(R.layout.activity_main);
	  
	  playPause = (ImageButton) findViewById(R.id.play);
	  stop = (ImageButton) findViewById(R.id.stop); 
	  text = (TextView) findViewById(R.id.textView);  
	  playerService("GET_STATUS");  
	  playPause.setOnClickListener(new View.OnClickListener() {
	   public void onClick(View view) {
		playerService("PLAY"); 
	   }
	  });
	  stop.setOnClickListener(new View.OnClickListener() {
	   public void onClick(View view) { 
	    playerService("STOP"); 
	   }
	  });
  }
  @Override
  public void onResume(){
	  super.onResume(); 
	  LocalBroadcastManager.getInstance(this).registerReceiver(mStatusReceiver, 
			  new IntentFilter("player-status-message"));  
  }
  @Override 
  public void onPause(){
	  LocalBroadcastManager.getInstance(this).unregisterReceiver(mStatusReceiver); 
	  super.onPause(); 
  }
}